package com.min.nio.rpc.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * create by sirmin on 2018/3/21
 */
public class MessageUtils {
    public static Object getMessage(SocketChannel channel) throws IOException, ClassNotFoundException {
        final ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        if (channel.read(byteBuffer) > 0) {
            byteBuffer.flip();
            final ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(byteBuffer.array(), 0, byteBuffer.limit()));
            byteBuffer.clear();
            return objectInputStream.readObject();
        }
        return null;
    }
}
